<?php

/**
 * 会员管理
 */

namespace app\ph\controller;

class User extends Base
{
    protected $user_model;

    public function __construct()
    {
        parent::__construct();
        $this->user_model   = new \app\common\model\User();
    }

    /**
     * 列表
     * @return \think\response\View
     * @throws \think\exception\DbException
     */
    public function index()
    {
        $where = [];
        $title = input('get.nick_name');
        $title && $where[] = ['nick_name', '=', $title];
        $data = $this->user_model->getList($where, 'id,nick_name,create_time');
        $page = $data->render();
        $data = $data->items();
        return view('user/index', [
            'data'      => $data,
            'page'      => $page,
            'nick_name' => $title
        ]);
    }

    /**
     * 修改
     * @param $id
     * @return \think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function data_update($id)
    {
        if (!$id) {
            $this->error('缺失参数');
        }
        $data = $this->user_model->getOne(['id' => $id], '*', 'id desc');
        if (!$data) {
            $this->error('数据获取失败');
        }
        return view('user/data_update', ['data' => $data]);
    }

    /**
     * 店铺修改
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function data_save_shop()
    {
        $userId = input('post.user_id');
        $status = input('shop_status');
        if (!$userId) {
            return json(['code' => 1, 'message' => '缺失用户ID参数']);
        }
        if (!$status) {
            return json(['code' => 1, 'message' => '请选择审核状态']);
        }
        switch ($status) {
            case '0':
                $content = '';
                break;
            case '1':
                $content = '您的店铺正在审核中';
                break;
            case '2':
                $content = '您的店铺已经通过审核';
                break;
            case '3':
                $content = '您的店铺存在违规内容，已被管理员关停';
                break;
            default:
                $content = '';
        }
        if ($content) {
            $this->notice_model->addData(
                [
                    'create_time' => time(),
                    'update_time' => time(),
                    'user_id'     => $this->userId,
                    'content'     => $content
                ]
            );
        }
        $res = $this->user_model->editData(['id' => $userId], ['shop_status' => $status, 'update_time' => time()]);
        return operationResult($res);
    }

    /**
     * 删除
     * @return \think\response\Json
     */
    public function data_delete()
    {
        $id = input('post.id');
        if (!$id) {
            return json(['code' => 100, 'msg' => '缺少参数ID']);
        }
        $res = $this->user_model->dataDelete($id);
        return operationResult($res);
    }
}
